﻿@page "/BusinessDepartmentPage"
@using System.ComponentModel
@using AntDesign.TableModels
@inject BusinessDepartmentServer BusinessDepartmentServer;
@inject MessageService _message

<div>
    <Button @onclick="@Resh" Type="@ButtonType.Primary" Icon="@IconType.Outline.Reload">刷新</Button>
    <Button Danger OnClick="@(() => { BusinessDepartmentEntityc = new BusinessDepartmentEntitycs(); _visibleadd = true; })">添加</Button>
    <Button Danger OnClick="@(() => {
                                  if (selectedRows != null && selectedRows.Any())
                                  {
                                      BusinessDepartmentEntityc = selectedRows.First().Clone();
                                      _visibleadd = true;
                                  }})">
        复制
    </Button>
    @if (selectedRows != null && selectedRows.Any())
    {
        <Button Danger OnClick="@(() => { _visible = true; })">删除</Button>
    }

</div>
<div>
    <Table @ref="table"
           TItem="BusinessDepartmentEntitycs"
           DataSource="@BusinessDepartments"
           Total="_total"
           @bind-PageIndex="_pageIndex"
           @bind-PageSize="_pageSize"
           @bind-SelectedRows="selectedRows">
        <Selection Key="@(context.id.ToString())" />
        <Column @bind-Field="@context.id" Sortable />
        <Column Title="业务部编码" Field="@context.Code">
            <Input @bind-Value=@context.Code />
        </Column>
        <Column Title="说明" Field="@context.Remark">
            <Input @bind-Value=@context.Remark />
        </Column>
        <ActionColumn>
            <Button @onclick="()=>OnChange(context)">更新</Button>
        </ActionColumn>
    </Table>

    <br />
    <p>PageIndex: @_pageIndex | PageSize: @_pageSize | Total: @_total</p>

    <br />
    <h5>selections:</h5>
    <Button OnClick="()=> { _pageIndex--; }">Previous page</Button>
    <Button OnClick="()=> { _pageIndex++; }">Next Page</Button>
    <Modal Title="@title"
           Visible="@_visible"
           OnOk="@HandleOk"
           OnCancel="@HandleCancel">
        <p>确认删除？</p>
    </Modal>
    <Modal Title="@titleadd"
           Visible="@_visibleadd"
           OnOk="@HandleOkadd"
           OnCancel="@HandleCanceladd">
        <Form Model="BusinessDepartmentEntityc">
            <FormItem Label="业务部编码">
                <Input @bind-Value=@context.Code />
            </FormItem>
            <FormItem Label="说明">
                <Input @bind-Value=@context.Remark />
            </FormItem>
        </Form>
    </Modal>
</div>

@using System.Text.Json;
@code {
            private bool ReshFlag = false;
            ObservableCollection<BusinessDepartmentEntitycs> BusinessDepartments { get; set; } = new ObservableCollection<BusinessDepartmentEntitycs>();

            IEnumerable<BusinessDepartmentEntitycs> selectedRows;
            ITable table;

            int _pageIndex = 1;
            int _pageSize = 10;
            int _total = 0;
            string title = "危险提示！！！";
            bool _visible = false;

            private async void HandleOk(MouseEventArgs e)
            {
        @foreach (var selected in selectedRows)
        {
            bool flag = await BusinessDepartmentServer.Delete(selected.id);
            if (flag)
            {
                _message.Info($"CODE{selected.Code}-删除成功");
            }
            else
            {
                _message.Info($"CODE{selected.Code}-删除失败");
            }
        }
        selectedRows = null;
        _visible = false;
        await init();
    }

    private void HandleCancel(MouseEventArgs e)
    {
        selectedRows = null;
        _visible = false;
    }

    private BusinessDepartmentEntitycs BusinessDepartmentEntityc = new BusinessDepartmentEntitycs();
    string titleadd = "添加业务部";
    bool _visibleadd = false;

    private async void HandleOkadd(MouseEventArgs e)
    {
        bool flag = await BusinessDepartmentServer.ADD(BusinessDepartmentEntityc);
        if (flag)
        {
            _message.Info("添加成功");
        }
        else
        {
            _message.Info("添加失败");
        }
        _visibleadd = false;
        await init();
    }

    private void HandleCanceladd(MouseEventArgs e)
    {

        _visibleadd = false;
    }

    protected override async Task OnInitializedAsync()
    {
        var List = await BusinessDepartmentServer.GetBusinesses();
        if (List != null)
        {
            foreach (var item in List)
            {
                BusinessDepartments.Add(item);
            }
        }
        base.OnInitialized();
    }


    public async void OnChange(BusinessDepartmentEntitycs BusinessDepartmentEntityc)
    {
        bool flag = await BusinessDepartmentServer.UpData(BusinessDepartmentEntityc);
        if (flag)
        {
           await _message.Info($"CODE{BusinessDepartmentEntityc.Code}-更新成功");
        }
        else
        {
          await  _message.Info($"CODE{BusinessDepartmentEntityc.Code}-更新失败");
        }
         await init();
    }

    public void RemoveSelection(int id)
    {
        // var selected = selectedRows.Where(x => x.Id != id);
        // selectedRows = selected;
    }

    private void Delete(int id)
    {
        //forecasts = forecasts.Where(x => x.Id != id).ToArray();
        // _total = forecasts.Length;
    }
    private async void Resh()
    {
        if (!ReshFlag)
        {
            ReshFlag = true;

            await init();
            ReshFlag = false;
        }

    }

    private async Task Clear()
    {
        if (!ReshFlag)
        {
            ReshFlag = true;
            await init();
            ReshFlag = false;
        }
    }

    async Task init()
    {
        BusinessDepartments.Clear();
        var List = await BusinessDepartmentServer.GetBusinesses();
        if (List != null)
        {
            await Task.Run(() =>
            {

                foreach (var item in List)
                {
                    BusinessDepartments.Add(item);
                }
                this.InvokeAsync(() =>
                {
                    this.StateHasChanged();
                });
            });
        }


    }

}